﻿using Common.Base;
using Models;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace SanmaoSystem.ViewModels
{
    public class RegisterViewModel : BindableBase
    {//定义用户名
        private string _userName="";
        public string UserName { get { return _userName; } set { SetProperty(ref _userName, value); } }
        //定义密码
        private string _password="";
        public string Password { get { return _password; } set { SetProperty(ref _password, value); } }

        //定义注册命令
        private DelegateCommand _register;
        public DelegateCommand Register { get => new DelegateCommand(RegisterAccount); }

        private void RegisterAccount()
        {
            if (UserName != "" && Password !="")
            {
               MemberIProvider memberIProvider= new MemberIProvider();
                MD5Utils mD5Utils = new MD5Utils();
               var list = memberIProvider.GetAll();
               var user= list.FirstOrDefault(item => item.Password==mD5Utils.ComputeHash(Password) && item.Name==UserName);
                if (user == null)
                {  
                   
                    Members members = new Members();
                    members.Name = UserName;
                    members.Password = mD5Utils.ComputeHash(Password);
                    members.Role = 0;
                    members.InsertDate = DateTime.Now;
                   if(memberIProvider.Insert(members) > 0)
                    {
                        UserName = "";
                        Password = "";
                        MessageBox.Show("注册成功！");
                    }
                }
                else MessageBox.Show("账号已存在，请重新注册！");
            }
            else MessageBox.Show("用户名或密码不能为空");
        }

        public RegisterViewModel(){
        
     }
    }
}
